<template>
  <view><button type="primary" plain="true" @tap="getUserInfo" open-type="getUserInfo">请求授权</button></view>
</template>

<script>
import regeneratorRuntime from '../../lib/runtime/runtime.js';
import { request } from '../../request/request.js';
import { getSetting, openSetting, uniGetUserInfo, uniShowToast, uniLogin } from '../../utils/utils.js';
export default {
  data() {
    return {
      encryptedData: '',
      rawData: '',
      iv: '',
      signature: '',
      code: ''
    };
  },
  methods: {
    async getUserInfo() {
      var _me = this;
      // #ifndef H5
      // 非 H5 平台
      // 【1.获取用户信息】
      try {
        var res3 = await uniGetUserInfo({ lang: 'zh_CN' });
        // console.log('[getUserInfo]回调', res3);
        _me.encryptedData = res3.encryptedData;
        _me.rawData = res3.rawData;
        _me.iv = res3.iv;
        _me.signature = res3.signature;
      } catch (error) {
        //TODO handle the exception
        console.log('获取用户信息失败', error);
        switch (error.errMsg) {
          case 'getUserInfo:fail scope unauthorized':
            var res4 = await uniShowToast({
              title: '没有获取用户信息权限',
              mask: true,
              icon: 'none',
              duration: 1000
            });
            console.log('[chooseLocation]', '没有获取用户信息权限');
            break;
          default:
            break;
        }
      }
      // 【2.获取登录信息】
      try {
        var res5 = await uniLogin();
        // console.log('[获取登录信息]回调', res5);
        _me.code = res5.code;
      } catch (error) {
        //TODO handle the exception
        console.log('登录失败', error);
      }
      // 【3.获取token】
      try {
        var res6 = await request({
          url: 'https://api-hmugo-web.itheima.net/api/public/v1/users/wxlogin',
          method: 'POST',
          data: {
            encryptedData: _me.encryptedData,
            rawData: _me.rawData,
            iv: _me.iv,
            signature: _me.signature,
            code: _me.code
          }
        });
        // console.log('[获取token]回调', res6);
      } catch (error) {
        //TODO handle the exception
        console.log('获取token失败', error);
      }

      // #endif

      // 模拟获取数据
      var res6 = {
        message: {
          user_id: 23,
          user_email_code: null,
          is_active: null,
          user_sex: '男',
          user_qq: '',
          user_tel: '',
          user_xueli: '本科',
          user_hobby: '',
          user_introduce: null,
          create_time: 1562221487,
          update_time: 1562221487,
          token: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
        },
        meta: {
          msg: '登录成功',
          status: 200
        }
      };

      var token = res6.message.token;
      console.log('token', token);
      uni.setStorageSync('token', token);
      uni.navigateBack({
        delta: 1
      });
    }
  }
};
</script>

<style lang="scss"></style>
